package glazedlists.chapter6;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.AdvancedTableModel;
import ca.odell.glazedlists.swing.GlazedListsSwing;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import ca.odell.issuezilla.Issue;
import glazedlists.chapter2.IssueComparator;
import glazedlists.chapter2.IssueTableFormat;
import glazedlists.chapter3.IssueTextFilterator;
import glazedlists.chapter5.UsersSelect;

import javax.swing.*;
import java.awt.*;

public class IssuesBrowser6 {

    // reads issues from a stream and populates the issues event list
    private IssuesLoader issuesLoader = new IssuesLoader();
    private EventList<Issue> issueEventList = issuesLoader.getIssues();

    public IssuesBrowser6() {
        issuesLoader.load();
    }

    public void display() {
        issueEventList.getReadWriteLock().readLock().lock();
        try {
            SortedList<Issue> sortedIssues = new SortedList<>(issueEventList, new IssueComparator());
            UsersSelect usersSelect = new UsersSelect(sortedIssues);
            FilterList<Issue> userFilteredIssues = new FilterList<>(sortedIssues, usersSelect);
            JTextField filterEdit = new JTextField(10);
            IssueTextFilterator filterator = new IssueTextFilterator();
            MatcherEditor<Issue> matcherEditor = new TextComponentMatcherEditor<Issue>(filterEdit, filterator);
            FilterList<Issue> textFilteredIssues = new FilterList<>(userFilteredIssues, matcherEditor);

            AdvancedTableModel<Issue> tableModel = GlazedListsSwing.eventTableModelWithThreadProxyList(textFilteredIssues,
                    new IssueTableFormat());
            JTable issueJTable = new JTable(tableModel);
            TableComparatorChooser.install(issueJTable, sortedIssues, TableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
            JScrollPane issueTableScrollPane = new JScrollPane(issueJTable);

            JScrollPane usersListScrollPane = new JScrollPane(usersSelect.getJList());

            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(new JLabel("Filter: "), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(5, 5, 5, 5), 0, 0));
            panel.add(filterEdit, new GridBagConstraints(0, 1, 1, 1, 0.15, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(5, 5, 5, 5), 0, 0));
            panel.add(new JLabel("Reported By: "), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(5, 5, 5, 5), 0, 0));
            panel.add(usersListScrollPane, new GridBagConstraints(0, 3, 1, 1, 0.15, 1.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(5, 5, 5, 5), 0, 0));
            panel.add(issueTableScrollPane, new GridBagConstraints(1, 0, 1, 4, 0.85, 1.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(5, 5, 5, 5), 0, 0));

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(540, 480);
            frame.getContentPane().add(panel);
            frame.setVisible(true);
        } finally {
            issueEventList.getReadWriteLock().readLock().unlock();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            IssuesBrowser6 issuesBrowser = new IssuesBrowser6();
            issuesBrowser.display();
        });
    }
}
